﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for FriendsDAL
/// </summary>
public class FriendsDAL: StandardConnectDB, IFriends
{
	public FriendsDAL()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public List<Friends> GetFriendOfUser(Users user)
    {

        List<Friends> Listfriends = new List<Friends>();
        Dictionary<string, object> paramist = new Dictionary<string, object>();
         paramist.Add("@pUserName", user.Username);
         DataTable dt = this.DTExecuteSelect("spGetFriendOfUser", paramist);
        foreach (DataRow dr in dt.Rows)
        {
            Listfriends.Add(this.ConvertObjectDataRowToFriends(dr));
        }

        return Listfriends;

    }
    public List<UserProfiles> GetAvatarMyFriendofUser(Users user)
    {
        List<UserProfiles> userp = new List<UserProfiles>();
        Dictionary<string, object> paramist = new Dictionary<string, object>();
        paramist.Add("@pUserName", user.Username);
        DataTable dt = this.DTExecuteSelect("spGetAvatarMyFriendOfUser", paramist);

        foreach (DataRow dr in dt.Rows)
        {
            //Convert objec DataRow to object UserProfiles
            userp.Add(this.ConvertObjectDataRowToUserProfiles(dr));
        }
        
        return userp;
    }
    public UserProfiles ConvertObjectDataRowToUserProfiles(DataRow dr)
    {
        UserProfiles userprofile = new UserProfiles();

      //  userprofile.IdUserProfiles = Convert.ToInt32(dr["IdUserProfiles"].ToString());


        string username = dr["Names"].ToString();
        IUsers iUsers = new UsersDAL();
        userprofile.User = iUsers.GetUserByUsername(username);
        userprofile.Avatar = dr["Avatar"].ToString();
 
        return userprofile;
    }
    public bool IsFriend(Users mine, Users myFriend)
    {
        return true;
    }

    public bool InsertFriend(Users mine, Users myFriend)
    {

        bool result = false;

        //Create param list store parameter execute store procedure
        Dictionary<string, object> paramList = new Dictionary<string, object>();
        //Add parameter to paramList
        paramList.Add("@pUsername", mine.Username);
        paramList.Add("@pMyFriendUsername",myFriend.Username);

        //Update object User for table Users in database
        result = this.ExecuteUpdate("spInsertFriend", paramList);

        return result;
    }
    

    public bool UpdateActiveFriend(Friends friend)
    {
        throw new NotImplementedException();
    }

    public bool DeleteFriendByUsername(Friends friend)
    {
        throw new NotImplementedException();
    }

    public Friends ConvertObjectDataRowToFriends(DataRow dr)

    {               
            Friends fr = new Friends();
            String username = dr["Username"].ToString();
            IUsers iuser = new UsersDAL();
            fr.User = iuser.GetUserByUsername(username);

            String MyfrienduserName = dr["MyFriendUsername"].ToString();
            IUsers iUser = new UsersDAL();
             fr.MyFriendUserName = iUser.GetUserByUsername(MyfrienduserName);

            fr.IsFriend = bool.Parse(dr["IsFriend"].ToString());     

             fr.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
             UserProfiles up = new UserProfiles();
        
           return fr;
    }
}